package com.atguigu.msmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.utils.StringUtils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.commonutils.utils.ConstantPropertiesUtil;
import com.atguigu.msmservice.service.MsmService;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MsmserviceImpl implements MsmService {
    @Override
    public boolean sendMsm(String phone, Map<String, String> map) {
        //0 判断手机号
        if(StringUtils.isEmpty(phone)) return false;
        //1 创建初始化对象
        DefaultProfile profile =
                DefaultProfile.getProfile("default",ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        //2 创建request对象
        CommonRequest request = new CommonRequest();
        //3 向request里设置参数
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "tfl网上课堂注册验证");
        request.putQueryParameter("TemplateCode", "SMS_195261349");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

        try {
            //4 调用初始化兑现方法实现发送
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response);
            //5 通过response获取发送是否成功
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
